unit WtQQMainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, jpeg, ShellAPI, CommCtrl,
  TlHelp32;

type
  TfrmWtQQMain = class(TForm)
    Image1: TImage;
    pbView: TProgressBar;
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    FCopying: Boolean;
    procedure SyncQQ;
    procedure OnSharpCopy(AStatus, ACurFile: string;
                         ATotalSize, ACurSize,
                         ACurFileAllSize, ACurFileCurSize: Int64;
                         ATotalCount, ACurCount: Integer;
                         ASpeed: Integer; ARemainTime: TDateTime);
  public
    { Public declarations }
  end;

var
  frmWtQQMain: TfrmWtQQMain;

implementation

uses {FunctionUnit, }SharpCopyUnit;

const
  QQRunPath = 'c:\QQ';
  QQExe = 'QQ.exe';

{$R *.dfm}


function FindProcess(AProcessName: string): Boolean;
var
  h: THandle;
  lpee: TProcessEntry32;
  found: Boolean;
  SL: TStrings;
begin
  Result := False;
  h := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  lpee.dwSize := SizeOf( TProcessEntry32 );
  SL := TStringList.Create;
  try
//  LogMenuMessage('Begin list process...');
    found := Process32First(h, lpee);
    while found do
    begin
  //    if UpperCase(AProcessName) = UpperCase(lpee.szExeFile) then
  //    begin
  //      Result := True;
  //      LogMenuMessage('Find process - ' + AProcessName);
  //      Exit;
  //    end;
  //      if AProcessHandle <> lpee.th32ProcessID then
  //        TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0), lpee.th32ProcessID), 0);
      SL.Add( lpee.szExeFile );
  //    Logmenumessage( 'list process - ' + lpee.szExeFile );
      found := Process32Next(h, lpee);
    end;

    CloseHandle(h);

//    LogMenuMessage('End list process...');
//    SL.SaveToFile(ExtractFilePath(ParamStr(0)) + 'FindProcess.txt');
    Result := (SL.IndexOf( AProcessName ) > -1);
  finally
    SL.Free;
  end;
//  if not Result then
//    LogMenuMessage('Find process failed - ' + AProcessName);
end;

procedure TfrmWtQQMain.FormCreate(Sender: TObject);
begin
//  BorderStyle := bsNone;
//  Width := 0;
//  Height := 0;
//  CopyDir(ExtractFilePath(ParamStr(0)), QQRunPath);
  FCopying := False;
end;

procedure TfrmWtQQMain.OnSharpCopy(AStatus, ACurFile: string; ATotalSize,
  ACurSize, ACurFileAllSize, ACurFileCurSize: Int64; ATotalCount,
  ACurCount, ASpeed: Integer; ARemainTime: TDateTime);
begin
  pbView.Min := 0;
  pbView.Max := ATotalSize div 1024;
  pbView.Position := ACurSize div 1024;
end;

procedure TfrmWtQQMain.SyncQQ;
begin
  FCopying := True;
  SendMessage(pbView.Handle, PBM_SETBARCOLOR, 0, $FCA864);
  if not FindProcess(QQExe) then
  with TSharpCopy.Create(ExtractFilePath(ParamStr(0)), QQRunPath) do
  try
    OnCopyFile := OnSharpCopy;
    DoStart;
  finally
    Free;
  end;
  ShellExecute(Self.Handle, 'open', PChar(IncludeTrailingPathDelimiter(QQRunPath) + QQExe),
                                    PChar(QQRunPath), nil, SW_SHOW);
  Application.Terminate;
end;

procedure TfrmWtQQMain.FormActivate(Sender: TObject);
begin
  if FCopying then
    Exit;

  
  SyncQQ;
end;

end.
